<?php namespace Modules\Web\Http\Controllers;

use Pingpong\Modules\Routing\Controller;

class QuotationController extends Controller {
	
	public function index()
	{
        /**
         * ECSHOP 报价单
         * ============================================================================
         * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
         * 网站地址: http://www.ecshop.com；
         * ----------------------------------------------------------------------------
         * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
         * 使用；不允许对程序代码以任何形式任何目的的再发布。
         * ============================================================================
         * $Author: liubo $
         * $Id: quotation.php 17217 2011-01-19 06:29:08Z liubo $
         */
        define('IN_ECS', true);

        require(dirname(__FILE__) . '/includes/init.php');

        $action  = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'default';
        if ($action == 'print_quotation')
        {
            $smarty->template_dir = DATA_DIR;
            $smarty->assign('shop_name', $_CFG['shop_title']);
            $smarty->assign('cfg',       $_CFG);
            $where = get_quotation_where($_POST);
            $sql = "SELECT g.goods_id, g.goods_name, g.shop_price, g.goods_number, c.cat_name AS goods_category,p.product_id,p.product_number,p.goods_attr".
                " FROM " . $ecs->table('goods') . " AS g LEFT JOIN " . $ecs->table('category') . " AS c ON g.cat_id = c.cat_id LEFT JOIN ".$ecs->table('products')."as p  On g.goods_id=p.goods_id" . $where . " AND is_on_sale = 1 AND is_alone_sale = 1 ";
            $goods_list = $db->getAll($sql);

            foreach($goods_list as $key=>$val)
            {
                if(!empty($val['product_id']))
                {
                    $goods_list[$key]['goods_number']=$val['product_number'];
                    $product_info=product_info($val['goods_attr'],$val['goods_id']);
                    $goods_list[$key]['members_price']  = $val['shop_price'];
                    $goods_list[$key]['shop_price'] += $product_info['attr_price'];
                    $goods_list[$key]['product_name']=$product_info['attr_value'];
                    $goods_list[$key]['attr_price']  =$product_info['attr_price'];
                }
                else
                {
                    $goods_list[$key]['members_price']  = $val['shop_price'];
                    $goods_list[$key]['product_name']='&nbsp;';
                    $goods_list[$key]['product_price']=0;
                }
                $goods_list[$key]['goods_key']=$key;
            }
            $user_rank = $db->getAll("SELECT * FROM " .$ecs->table('user_rank') . "WHERE show_price = 1 OR rank_id = '$_SESSION[user_rank]'");
            $rank_point = 0;
            if (!empty($_SESSION['user_id']))
            {
                $rank_point = $db->getOne("SELECT rank_points FROM " . $ecs->table('users') . " WHERE user_id = '$_SESSION[user_id]'");
            }
            $user_rank = calc_user_rank($user_rank, $rank_point);
            $user_men = serve_user($goods_list);
            $smarty->assign('extend_price', $user_rank['ext_price']);
            $smarty->assign('extend_rank', $user_men);
            $smarty->assign('goods_list', $goods_list);

            $html = $smarty->fetch('quotation_print.html');
            exit($html);
        }

        assign_template();

        $position = assign_ur_here(0, $_LANG['quotation']);
        $smarty->assign('page_title', $position['title']);   // 页面标题
        $smarty->assign('ur_here',    $position['ur_here']); // 当前位置

        $smarty->assign('cat_list', cat_list());
        $smarty->assign('brand_list',   get_brand_list());

        if (is_null($smarty->get_template_vars('helps')))
        {
            $smarty->assign('helps', get_shop_help()); // 网店帮助
        }

        $smarty->display('quotation.dwt');

		return view('web::index');
	}
	
}